declare global {
  interface Window {
    __releaseLock(): void
  }
}

let lockPromise: Promise<void> | null = null

export const requestLock = async () => {
  while (lockPromise) {
    await lockPromise
  }
  lockPromise = new Promise((resolve) => {
    // 锁持有中
    window.__releaseLock = () => {
      lockPromise = null
      resolve()
    }
  })
}

export const releaseLock = () => {
  if (window.__releaseLock) window.__releaseLock()
}
